package com.sl.biz.modular.restapi.dao.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.biz.modular.restapi.dao.entity.ApiConfigDO;
import com.sl.biz.modular.restapi.dao.enums.ApiTypeCodeEnum;
import com.sl.biz.modular.restapi.dao.mapper.ApiConfigMapper;
import com.sl.biz.modular.restapi.dao.service.ApiConfigDaoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ApiConfigDaoServiceImpl extends ServiceImpl<ApiConfigMapper, ApiConfigDO> implements ApiConfigDaoService {

    @Override
    public List<ApiConfigDO> queryRestApiList(String apiId) {
        LambdaQueryWrapper<ApiConfigDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiConfigDO::getApiTypeCode, ApiTypeCodeEnum.REST.getCode());

        if (StrUtil.isNotBlank(apiId)) {
            List<String> ids = new ArrayList<>();
            ids.add(apiId);
            queryWrapper.in(ApiConfigDO::getId, ids);
        }
        queryWrapper.eq(ApiConfigDO::getThirdApiFlag, false);
        return super.list(queryWrapper);
    }

    @Override
    public Boolean setApiStatusCode(String id, String apiStatusCode) {
        LambdaUpdateWrapper<ApiConfigDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApiConfigDO::getId, id);
        updateWrapper.set(ApiConfigDO::getApiStatusCode, apiStatusCode);
        return super.update(updateWrapper);
    }
}
